<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="商品金额类型" prop="goodsStatus">
        <el-select v-model="queryParams.goodsStatus" clearable size="small">
            <el-option v-for="dict in dict.type.orderType" v-if="dict.value!=10" :key="dict.value" :label="dict.label" :value="dict.value">
            </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="商品名称" prop="goodsName">
        <el-input
          v-model="queryParams.goodsName"
          placeholder="请输入商品名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['pos:TTerminalGoodsInfo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['pos:TTerminalGoodsInfo:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['pos:TTerminalGoodsInfo:remove']"
        >删除</el-button>
      </el-col>
<!--      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['pos:TTerminalGoodsInfo:export']"
        >导出</el-button>
      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="TTerminalGoodsInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="150" align="center" />
      <el-table-column label="主键id" align="center" prop="id" v-if="false" />
      <el-table-column label="商品品牌" align="center" prop="brandIdShow"  v-if="false"/>
      <el-table-column label="商品图片" align="center" prop="goodsImg"  >
        <template   slot-scope="scope">
          <el-popover placement="left" trigger="click" width="400">
            <img :src="scope.row.goodsImg" width="100%" />
            <img
              slot="reference"
              :src="scope.row.goodsImg"
              :alt="scope.row.goodsImg"
              style="max-height: 70px;max-width: 70px; padding:                     5px"
            />
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="商品类型" align="center" prop="goodsType" v-if="false"/>
      <el-table-column label="商品金额类型" align="center" prop="goodsStatus" />
      <el-table-column label="商品名称" align="center" prop="goodsName" />
      <el-table-column label="兑换金额" align="center" prop="amount" >
        <template slot-scope="scope">
          {{scope.row.amount}}{{scope.row.exchangeUnit}}/{{scope.row.unit}}
        </template>
      </el-table-column>
      <el-table-column label="兑换商品邮费" align="center" prop="deliveryFee" />
<!--      <el-table-column label="商品单位" align="center" prop="unit" />-->
<!--      <el-table-column label="商品单位" align="center" prop="exchangeUnit" />-->
      <el-table-column label="商品排序" align="center" prop="sort" />
      <el-table-column label="商品状态" align="center" prop="isValid" >
        <template slot-scope="scope">
          <p v-if="scope.row.isValid==0">正常</p>
          <p v-if="scope.row.isValid==1">禁用</p>
        </template>
      </el-table-column>
      <!--<el-table-column label="文字介绍" align="center" prop="describe" :show-overflow-tooltip="true"/>-->
      <!--<el-table-column label="产品兑换说明" align="center" prop="conversionExplain" :show-overflow-tooltip="true"/>-->
      <!--<el-table-column label="产品说明" align="center" prop="goodsExplain" :show-overflow-tooltip="true" />-->
      <el-table-column label="是否热门推荐" align="center" prop="isPopular" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['pos:TTerminalGoodsInfo:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['pos:TTerminalGoodsInfo:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商品基础对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="终端购买品牌" prop="brandId">
          <el-input v-model="form.brandId" placeholder="请输入终端购买品牌id" />
        </el-form-item>
        <el-form-item label="商品类型" v-if="false">
          <el-radio-group v-model="form.goodsType">
            <el-option label="兑换商品" value="0" />
          </el-radio-group>
        </el-form-item>
        <el-form-item label="金额类型" prop="goodsStatus">
          <el-select v-model="form.goodsStatus" placeholder="请选择0:采购(终端支付),1:积分，2:交易金序列号，3:奖励金序列号，4:金币">
            <el-option label="采购商品" value="0" />
            <el-option label="积分" value="1" />
            <el-option label="交易金序列号" value="2" />
            <el-option label="奖励金序列号" value="3" />
            <el-option label="金币" value="4" />
          </el-select>
        </el-form-item>
        <el-form-item label="商品名称" prop="goodsName">
          <el-input v-model="form.goodsName" placeholder="请输入商品名称" />
        </el-form-item>
        <el-form-item label="商品主图片" prop="goodsImg">
          <imageUpload   v-model="form.goodsImg"/>
        </el-form-item>
        <el-row>
          <el-col :span="8">
            <el-form-item label="兑换/购买金额" prop="amount">
              <el-input v-model="form.amount" placeholder="请输入兑换/购买金额" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
              <el-input v-model="form.exchangeUnit" placeholder="请输入金额单位（元，积分，金币）" />
          </el-col>
            <el-col :span="8">
              <el-input v-model="form.unit" placeholder="请输入商品单位" />
          </el-col>
        </el-row>
        <el-form-item label="兑换商城商品" prop="deliveryFee">
          <el-input v-model="form.deliveryFee" placeholder="请输入兑换商城商品" />
        </el-form-item>
        <el-form-item label="商品排序" prop="sort">
          <el-input v-model="form.sort" placeholder="请输入商品排序" />
        </el-form-item>
        <el-form-item label="商品状态">
          <el-radio-group v-model="form.isValid">
            <el-radio label="0">正常</el-radio>
            <el-radio label="1">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="文字介绍" prop="describe">
          <el-input v-model="form.describe" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="产品兑换说明">
          <editor v-model="form.conversionExplain" :min-height="192"/>
        </el-form-item>
        <el-form-item label="产品说明">
          <editor v-model="form.goodsExplain" :min-height="192"/>
        </el-form-item>
        <el-form-item label="是否热门推荐">
          <el-radio-group v-model="form.isPopular">
            <el-radio label="0">非热门</el-radio>
            <el-radio label="1">热门</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="商品轮播图">
          <imageUpload :limit="6"  v-model="form.goodsImgS"/>
          <el-input v-model="form.goodsImgS" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { listTTerminalGoodsInfoExchange, getTTerminalGoodsInfo, delTTerminalGoodsInfo, addTTerminalGoodsInfo, updateTTerminalGoodsInfo, exportTTerminalGoodsInfo } from "@/api/pos/TTerminalGoodsInfoExchange";

  export default {
    name: "TTerminalGoodsInfo",
    dicts: ['orderType'],
    data() {
      return {
        // 遮罩层
        loading: true,
        // 导出遮罩层
        exportLoading: false,
        // 选中数组
        ids: [],
        name:[],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 商品基础表格数据
        TTerminalGoodsInfoList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          brandId: null,
          goodsType: 1,
          goodsStatus: null,
          goodsName: null,
          amount: null,
          deliveryFee: null,
          unit: null,
          sort: null,
          isValid: null,
          describe: null,
          conversionExplain: null,
          goodsExplain: null,
          isPopular: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询商品基础列表 */
      getList() {
        this.loading = true;
        listTTerminalGoodsInfoExchange(this.queryParams).then(response => {
          this.TTerminalGoodsInfoList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          brandId: null,
          goodsType: null,
          goodsStatus: null,
          goodsName: null,
          amount: null,
          deliveryFee: null,
          unit: null,
          sort: null,
          isValid: "0",
          describe: null,
          createTime: null,
          conversionExplain: null,
          goodsExplain: null,
          isPopular: "0",
          goodsImg: null,
          exchangeUnit:null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.name = selection.map(item => item.goodsName)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.$router.push({path: '/TTerminalGoodsInfo/exchange/add_exchange', query: {}});
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        this.$router.push({path: '/TTerminalGoodsInfo/exchange/edit_exchange', query: {"id": id}});

      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateTTerminalGoodsInfo(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addTTerminalGoodsInfo(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        const name = row.goodsName || this.name
        this.$modal.confirm('是否确认删除？').then(function() {
          return delTTerminalGoodsInfo(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
    }
  };
</script>
